﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.ImportRequest
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Principal;
using System.Text;

namespace Microsoft.InfoCards
{
  internal class ImportRequest : ClientUIRequest
  {
    private FileStream m_importFile;
    private string m_filename;

    public ImportRequest(
      Process callingProcess,
      WindowsIdentity callingIdentity,
      InfoCardUIAgent uiAgent,
      IntPtr rpcHandle,
      Stream inArgs,
      Stream outArgs)
      : base(callingProcess, callingIdentity, uiAgent, rpcHandle, inArgs, outArgs, InfoCardUIAgent.CallMode.Import, ExceptionList.AllNonFatal)
    {
    }

    public string ImportedFile
    {
      get
      {
        return this.m_filename;
      }
    }

    protected override void OnMarshalInArgs()
    {
      string path = Utility.DeserializeString((BinaryReader) new InfoCardBinaryReader(this.InArgs, Encoding.Unicode));
      if (string.IsNullOrEmpty(path) || path.Length > 259)
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFileName")));
      if (!Path.IsPathRooted(path))
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("InvalidImportFileName")));
      try
      {
        this.m_filename = path;
        this.m_importFile = new FileStream(this.m_filename, FileMode.Open, FileAccess.Read, FileShare.Read);
      }
      catch (ArgumentException ex)
      {
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("CannotOpenImportFile"), (Exception) ex));
      }
      catch (IOException ex)
      {
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("CannotOpenImportFile"), (Exception) ex));
      }
      catch (NotSupportedException ex)
      {
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("CannotOpenImportFile"), (Exception) ex));
      }
      catch (UnauthorizedAccessException ex)
      {
        throw InfoCardTrace.ThrowHelperError((Exception) new ImportException(SR.GetString("CannotOpenImportFile"), (Exception) ex));
      }
    }

    protected override void OnProcess()
    {
      this.StartAndWaitForUIAgent();
    }

    protected override void OnMarshalOutArgs()
    {
    }

    protected override void OnDisposeAsUser()
    {
      base.OnDisposeAsUser();
      if (this.m_importFile == null)
        return;
      this.m_importFile.Dispose();
      this.m_importFile = (FileStream) null;
    }

    protected override bool OnHandleException(Exception e, out int errorCode)
    {
      errorCode = 0;
      bool flag = false;
      if (e is UserCancelledException)
      {
        errorCode = (e as UserCancelledException).NativeHResult;
        flag = true;
      }
      return flag;
    }
  }
}
